﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PublicTransport.DAL.DTO;
using System.Collections;

namespace PublicTransport.Views
{
    public partial class NearestStationsView : Form, INearestView
    {
        public event EventHandler FormClosing;
        public event EventHandler setStartStation;
        public event EventHandler setDestinationStation;

        public NearestStationsView()
        {
            InitializeComponent();
        }

        struct StationToDisplay
        {
            public StationDTO station;
            public double distance;

            public override string ToString()
            {
                String totalDistance = null;
                //double distanceInMeters = distance * 100000;
                if (distance < 1)
                    totalDistance = ((int)(distance*1000)).ToString() + "m";
                else
                    totalDistance = ((int)(distance)).ToString() + "km";

                return station.Name + " (" +totalDistance+ ")";
            }
        }

        public void DisplayStations(SortedList<double, PublicTransport.DAL.DTO.StationDTO> stations)
        {
            List<StationToDisplay> stationsToDisplay = new List<StationToDisplay>();            
	        //Now you have a DictionaryEntry object that allows 
	        //you to iterate and get all the key value pairs. 
            foreach (KeyValuePair<double, StationDTO> key in stations) 
	        {
                stationsToDisplay.Add(new StationToDisplay() { station = key.Value, distance = key.Key});
	        }

            //stationsListBox.DisplayMember = "value";
            this.Invoke((System.Threading.ThreadStart)delegate
            {
                waitingLbl.Visible = false;
                stationsListBox.DataSource = stationsToDisplay.ToArray();
                stationsListBox.Refresh();
              
            });



        }

        private void NearestStationsView_Closing(object sender, CancelEventArgs e)
        {
            if (FormClosing != null)
                FormClosing(this, new EventArgs());
        }

        public void ShowForm()
        {
            this.Show();
        }

        private void destinationSetmenuItem1_Click(object sender, EventArgs e)
        {
            if (setDestinationStation != null)
                setDestinationStation(this, new EventArgs());
        }

        private void startMenuItem2_Click(object sender, EventArgs e)
        {
            if (setStartStation != null)
                setStartStation(this, new EventArgs());
        }


        public string getStation()
        {
            if (stationsListBox.SelectedItem == null)
            {
                return null;
            }
            else
            {
                StationToDisplay station = (StationToDisplay)stationsListBox.SelectedItem;
                return station.station.Name;
            }
        }


    }
}